package com.example.seabedmusic.entity;

import java.util.ArrayList;
import java.util.List;

public class Playlist {
    private String id;
    private String name;
    private String description;
    private String cover; // 歌单封面
    private int songCount; // 歌曲数量

    private List<Song> songs; // 歌单中的歌曲列表

    public Playlist() {
        this.songs = new ArrayList<>();
    }

    public Playlist(String id, String name, String description, String cover) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.cover = cover;
        this.songs = new ArrayList<>();
        this.songCount = 0;
    }

    // Getter and Setter methods
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }

    public String getCover() { return cover; }
    public void setCover(String cover) { this.cover = cover; }

    public int getSongCount() { return songCount; }
    public void setSongCount(int songCount) { this.songCount = songCount; }

    public List<Song> getSongs() { return songs; }
    public void setSongs(List<Song> songs) {
        this.songs = songs;
        this.songCount = songs != null ? songs.size() : 0;
    }

    public void addSong(Song song) {
        if (songs == null) {
            songs = new ArrayList<>();
        }
        songs.add(song);
        songCount = songs.size();
    }

    /**
     * 从歌单移除歌曲
     * @param song 要移除的歌曲
     * @return 是否成功移除
     */
    public boolean removeSong(Song song) {
        if (songs != null) {
            boolean removed = songs.remove(song);
            if (removed) {
                songCount = songs.size();
            }
            return removed;
        }
        return false;
    }

    /**
     * 根据歌曲ID移除歌曲
     * @param songId 歌曲ID
     * @return 是否成功移除
     */
    public boolean removeSongById(String songId) {
        if (songs != null) {
            for (Song song : songs) {
                if (song.getId().equals(songId)) {
                    return removeSong(song);
                }
            }
        }
        return false;
    }

    /**
     * 检查歌单是否包含指定歌曲
     * @param song 要检查的歌曲
     * @return 是否包含
     */
    public boolean containsSong(Song song) {
        if (songs == null || song == null) return false;

        for (Song s : songs) {
            if (s.getId().equals(song.getId())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据歌曲ID检查歌单是否包含歌曲
     * @param songId 歌曲ID
     * @return 是否包含
     */
    public boolean containsSongById(String songId) {
        if (songs == null) return false;

        for (Song song : songs) {
            if (song.getId().equals(songId)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String toString() {
        return "Playlist{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", cover='" + cover + '\'' +
                ", songCount=" + songCount +
                '}';
    }
}